package tingshu.servlet.user;

import tingshu.service.UserService;
import tingshu.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/studio/user/register.do")
public class RegisterDoServlet extends HttpServlet {
    private final UserService userService = new UserService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if (username == null || password == null || username.length() > 20 || password.length() > 60) {
            resp.sendRedirect("/studio/user/register.do");
        }

        UserVO userVO = userService.register(username,password);

        HttpSession session = req.getSession();
        session.setAttribute("currentUser",userVO);

        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/plain");
        resp.getWriter().println("注册成功");
        //resp.sendRedirect("/studio");
    }
}
